<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    class EventEmitter {
        #listener = {}

        on (name, fn) {
            if (!this.#listener[name]) {
                this.#listener[name] = new Set()
            }
            this.#listener[name].add(fn)
        }

        once(name, callback) {
            const onceWrapper = (...args) => {
                callback(...args)
                this.off(name, onceWrapper)
            }
            this.on(name, onceWrapper)
        }

        off (name, callback) {
            if (!this.#listener[name]) return
            const callbacks = this.#listener[name]
            callbacks.delete(callback)
            if (callbacks.size === 0) {
                delete this.#listener[name]
            }
        }

        emit (name, ...args) {
            if (!this.#listener[name]) {
                return '没有该类型'
            }
            this.#listener[name].forEach(fn => {
                fn(...args)
            });
        }
    }

    const event = new EventEmitter()

    function logMessage(e) {
        console.log('数据', e)
    }
    function logMessage2(e) {
        console.log('数据', e)
    }

    event.once('click', logMessage)
    event.once('click', logMessage2)


    // event.off('click', logMessage)


    // setTimeout(() => {
    //     event.emit('click', 1)
    // }, 3000)

</script>
</html>